package com.kun.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.kun.music.domain.Singer;
import com.kun.music.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author fangkun
 * @create 2022-12-21-11:46
 */
@RestController
public class SingerController {

    @Configuration
    public class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/img/singerPic/**").addResourceLocations("file:E:\\workSpace\\music-website-master\\music-server\\img\\singerPic\\");
        }
    }

    @Autowired
    private SingerService singerService;

    @GetMapping("/singer")
    public Object allSinger(){
        return singerService.allSinger();
    }

    @PostMapping("singer/add")
    public Object addSinger(HttpServletRequest request){

        JSONObject object = new JSONObject();

        String name = request.getParameter("name").trim();
        String sex = request.getParameter("sex").trim();
        String pic = request.getParameter("pic").trim();
        String birth = request.getParameter("birth").trim();
        String location = request.getParameter("location").trim();
        String introduction = request.getParameter("introduction").trim();

        Singer singer = new Singer();
        singer.setName(name);
        singer.setSex(new Byte(sex));
        singer.setPic(pic);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date myBirth = new Date();
        try {
            myBirth = dateFormat.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        singer.setBirth(myBirth);

        singer.setLocation(location);
        singer.setIntroduction(introduction);

        boolean res = singerService.addSinger(singer);

        if (res){
            object.put("code", 1);
            object.put("msg", "添加成功");
            return object;
        }else {
            object.put("code", 0);
            object.put("msg", "添加失败");
            return object;
        }
    }

    @PostMapping("/singer/avatar/update")
    public Object updateSingerPic(@RequestParam("file") MultipartFile avatorFile,@RequestParam("id") int id){

        JSONObject jsonObject = new JSONObject();

        if (avatorFile.isEmpty()){
            jsonObject.put("code",0);
            jsonObject.put("msg","文件上传失败！");
            return jsonObject;
        }

        String fileName = System.currentTimeMillis() + avatorFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "music-server" + System.getProperty("file.separator") +"avatorImages" ;

        File file = new File(filePath);
        if (!file.exists()){
            file.mkdir();
        }

        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String storeAvatorPath = "/avatorImages/"+fileName;

        try {
            avatorFile.transferTo(dest);
            Singer singer = new Singer();
            singer.setId(id);
            singer.setPic(storeAvatorPath);
            boolean res = singerService.updateSingerPic(singer);
            if (res){
                jsonObject.put("code", 1);
                jsonObject.put("pic", storeAvatorPath);
                jsonObject.put("msg", "上传成功");
                return jsonObject;
            }else {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败");
                return jsonObject;
            }
        }catch (IOException e){
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败" + e.getMessage());
            return jsonObject;
        }finally {
            return jsonObject;
        }
    }

    @PostMapping("singer/update")
    public Object updateSingerInfo(HttpServletRequest request){
        JSONObject object = new JSONObject();

        String id = request.getParameter("id");
        String name = request.getParameter("name").trim();
        String sex = request.getParameter("sex").trim();
        String pic = request.getParameter("pic").trim();
        String birth = request.getParameter("birth").trim();
        String location = request.getParameter("location").trim();
        String introduction = request.getParameter("introduction").trim();

        Singer singer = new Singer();
        singer.setId(Integer.parseInt(id));
        singer.setName(name);
        singer.setSex(new Byte(sex));
        singer.setPic(pic);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date myBirth = new Date();
        try {
            myBirth = dateFormat.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        singer.setBirth(myBirth);

        singer.setLocation(location);
        singer.setIntroduction(introduction);

        boolean res = singerService.updateSingerInfo(singer);

        if (res){
            object.put("code", 1);
            object.put("msg", "修改成功");
            return object;
        }else {
            object.put("code", 0);
            object.put("msg", "修改失败");
            return object;
        }
    }

    @GetMapping("singer/delete")
    public Object deleteSinger(@RequestParam("id") int id){
        JSONObject object = new JSONObject();

        boolean res = singerService.deleteSinger(id);

        if (res){
            object.put("code", 1);
            object.put("msg", "删除成功");
            return object;
        }else {
            object.put("code", 0);
            object.put("msg", "删除失败");
            return object;
        }
    }

    @GetMapping("/singer/name/detail")
    public Object singerByName(HttpServletRequest request){
        String name = request.getParameter("name").trim();
        return singerService.singerByName(name);
    }

}
